
#Load Packages
library(ggplot2)
library(gridExtra)
library(dataverse)

# Set this Working Directory to the Folder with the Replication Files
#setwd("C:/Steven/GDrive/Research Files/Clark-Rogers/Descriptive Representation/APSR Final Submission/Replication")

#  Load Data

state.scatter <- get_dataframe_by_name(
  filename = "figure_1a.tab",
  dataset = "10.7910/DVN/QYZGEI", 
  server = "dataverse.harvard.edu")

cong.density <- get_dataframe_by_name(
  filename = "figure_1b.tab",
  dataset = "10.7910/DVN/QYZGEI", 
  server = "dataverse.harvard.edu")

#state.scatter<-read.dta13("Data/Stauffer Files/figure_1a.dta")
#cong.density<-read.dta13("Data/Stauffer Files/figure_1b.dta")

#############################################################
#### Figure 1a: Scatter Plot ####
#############################################################
head(state.scatter)

scatter<-ggplot(state.scatter, aes(x=state_guess, y=state)) +
  geom_point(size=4, shape=1)+
  geom_smooth(method = "lm", size = 1.5, color="grey45")+
  ylab("% Women in R's State Leg. \n")+
  xlab("\n Respondent Estimates")+
  ggtitle(" ")+
  theme_bw()
scatter<-scatter+theme_bw() + theme(panel.grid.major = element_blank(),
                                    panel.grid.minor = element_blank(), panel.border=element_blank())
scatter<-scatter+  theme(axis.text=element_text(size=17),
                         axis.title=element_text(size=17, face="bold"),
                         plot.title=element_text(size=19, face="bold"))+
  scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), labels=c("0", "10", "20", "30", "40", "50+"), limits=c(0, 50))
scatter<-scatter+theme(legend.justification=c(.5,.85), legend.position=c(.05,.95), legend.text=element_text(size=14),
                       legend.title=element_text(size=16))
scatter<-scatter+theme(legend.key.width=unit(2,"cm"))
scatter_stauffer_gender <-scatter+theme(axis.line.x=element_line(color="black", size=.5), axis.line.y=element_line(color="black", size=.5))

scatter

#############################################################
#### Figure 1b: Density Plot ####
#############################################################
cong.density$r_estimate<-ifelse(cong.density$r_estimate>50, 50, cong.density$r_estimate)
table(cong.density$r_estimate)

density<-ggplot(cong.density, aes(r_estimate))+
  geom_density(adjust=1, size=1.5, alpha=.45)
density<-density+scale_fill_manual(values=c("white"))
#density<-density+scale_color_manual(values=c("grey39", "grey55"), name="Year", labels=c("2016", "2018"))
density<-density+labs(x="\n Estimate % Women in Congress", y=" \n \n Density \n",
                      title= )
density<-density+theme_bw()+scale_x_continuous(breaks=c(0, 10, 20, 30, 40, 50), labels=c("0", "10", "20", "30", "40", "50+"), limits=c(0, 50))
density<-density + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
                         panel.background = element_blank(), panel.border=element_blank(), axis.line = element_line(colour = "black"))
density_stauffer_gender <-density+theme(axis.text=element_text(size=17),
                                        axis.title=element_text(size=17, face="bold"),
                                        plot.title=element_text(size=19, face="bold"))


tiff(filename = "Figures/Appendix/Figure-A-1.tiff",
    width = 12, height = 6, units = "in", pointsize = 12, res=1200)

test <- grid.arrange(density_stauffer_gender, scatter_stauffer_gender,  ncol=2)

dev.off()